package com.mm.cloud.sys.controller;

import com.mm.cloud.common.core.result.Result;
import com.mm.cloud.common.security.annotation.PreAuthorize;
import com.mm.cloud.sys.dto.form.RoleDTO;
import com.mm.cloud.sys.dto.form.RoleToPermissionDTO;
import com.mm.cloud.sys.dto.query.QueryRoleDTO;
import com.mm.cloud.sys.entity.Role;
import com.mm.cloud.sys.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色基础信息 前端控制器
 * </p>
 *
 * @author mr
 * @since 2020-09-15
 */
@Tag(name = "系统角色基础信息")
@RestController
@AllArgsConstructor
@RequestMapping("/sys/role")
public class RoleController {

    /**
     * 系统角色基础信息
     */
    private final IRoleService roleService;

    @Operation(summary = "分页查询【mr】", description = "分页查询")
    @PostMapping("getRoleList")
    @PreAuthorize(hasAuthorize = "ROLE_ROLE_LIST")
    public Result<List<Role>> getRoleList(@RequestBody QueryRoleDTO dto) {
        return roleService.getRoleList(dto);
    }

    @Operation(summary = "新增角色【mr】", description = "新增角色")
    @PostMapping("/saveRole")
    @PreAuthorize(hasAuthorize = "ROLE_ROLE_ADD")
    public Result<String> saveRole(@RequestBody RoleDTO dto) {
        return roleService.saveRole(dto);
    }

    @Operation(summary = "修改角色【mr】", description = "修改角色")
    @PostMapping("/updateRole")
    @PreAuthorize(hasAuthorize = "ROLE_ROLE_EDIT")
    public Result<String> updateRole(@RequestBody RoleDTO dto) {
        return roleService.updateRole(dto);
    }

    @Operation(summary = "删除角色【mr】", description = "删除角色")
    @PostMapping("/deleteRole/{roleId}")
    @PreAuthorize(hasAuthorize = "ROLE_ROLE_DELETE")
    public Result<String> deleteRole(@PathVariable("roleId") Long roleId) {
        return roleService.deleteRole(roleId);
    }

    @Operation(summary = "根据角色ID查询拥有的资源权限【mr】", description = "根据角色ID查询拥有的资源权限")
    @PostMapping("/getPermissionIdByRoleId/{roleId}")
    public Result<List<Long>> getPermissionIdByRoleId(@PathVariable("roleId") Long roleId) {
        return roleService.getPermissionIdByRoleId(roleId);
    }

    @Operation(summary = "角色资源设置【mr】", description = "角色资源设置")
    @PostMapping("/setRoleToPermission")
    @PreAuthorize(hasAuthorize = "ROLE_ROLE_SET")
    public Result<String> setRoleToPermission(@RequestBody RoleToPermissionDTO dto) {
        return roleService.setRoleToPermission(dto);
    }

}

